package j3dscene;

import java.io.File;
import javax.sound.sampled.*;


/**
 * Realizes the playing of different drumsounds
 * @author Steffen G.
 * @author Dominik K.
 */
public class Sound {

    /**
     * used to identify the playable sound on harddisc
     */
    java.net.URL filename;

    /**
     * Creates a new instance of SoundTest
     */
    public Sound() {
    }

    /**
     * plays sound dependend on its type
     * 
     * @param soundType 
     */
    public void playSound(int soundType) {

        switch (soundType) {
            case 1:
                filename = ClassLoader.getSystemResource("files/Snare.wav");
                break;
            case 2:
                filename = ClassLoader.getSystemResource("files/Bassdrum.wav");
                break;
            case 3:
                filename = ClassLoader.getSystemResource("files/HihatOpen.wav");
                break;
            case 4:
                filename = ClassLoader.getSystemResource("files/HihatClosed.wav");
                break;
            case 5:
                filename = ClassLoader.getSystemResource("files/Crash.wav");
                break;
            case 6:
                filename = ClassLoader.getSystemResource("files/Ride.wav");
                break;
            case 7:
                filename = ClassLoader.getSystemResource("files/Racktom.wav");
                break;
            case 8:
                filename = ClassLoader.getSystemResource("files/Floortom.wav");
                break;
            default:
                filename = ClassLoader.getSystemResource("files/test.wav");
                break;
        }

        try {
            //Create new Audiostream and Audioformat
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(filename);
            AudioFormat af = audioInputStream.getFormat();
            
            //get Data
            int size = (int) (af.getFrameSize() * audioInputStream.getFrameLength());
            byte[] audio = new byte[size];
            DataLine.Info info = new DataLine.Info(Clip.class, af, size);
            
            //read Audiostream
            audioInputStream.read(audio, 0, size);
            
            //create, open and play clip/audio
            Clip clip = (Clip) AudioSystem.getLine(info);
            clip.open(af, audio, 0, size);
            clip.start();
            
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}